#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	int point=0,num=0;
	int sum=0;
	int n1=0,n2=0,cost=0;
	vector<map<int,int> > vec;
	scanf("%d %d",&point,&num);
	for(int i=0;i<point*2;i++)
	{
		map<int,int> map;
		vec.push_back(map);
	}
	for(int i=0;i<num;i++)
	{
		scanf("%d %d %d",&n1,&n2,&cost);
		vec[n1-1].insert(pair<int,int>(cost,n2-1));
		vec[n2-1].insert(pair<int,int>(cost,n1-1));
	}
	vector<int> p(point,0);
	//cout<<"2"<<endl;
	int flag=1;
	int temp=0;
	p[temp]=1;
	map<int,int>::iterator miter;
	for(int i=0;i<num;i++)
	{
		for(miter=vec[temp].begin();miter!=vec[temp].end();miter++)
		{
			if(p[miter->second]==0)
			{
				p[miter->second]=1;
				sum+=miter->first;
				temp=miter->second;
				break;
			}
		}
	}
	for(int i=0;i<point;i++)
	{
		if(p[i]==0)
			sum+=vec[i].begin()->first;
		p[i]=1;
	}
	printf("%d",sum);
	system("pause");
	return 0;
}
